package com.tfjybj.itoo.teach.provider.facade;

import org.apache.dubbo.config.annotation.Service;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.FacadeApi.SemesterApi;
import com.tfjybj.itoo.teach.entity.SemesterEntity;
import com.tfjybj.itoo.teach.model.SemesterModel;
import com.tfjybj.itoo.teach.provider.service.SemesterService;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DUYU
 * @date 2019年5月22日16:04:33
 */
@Service
@org.springframework.stereotype.Service
public class SemesterFacade implements SemesterApi {

    @Resource
    private SemesterService semesterService;



    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model SemesterModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
   public ItooResult create(SemesterModel model){
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        SemesterEntity semesterEntity = new SemesterEntity();
        BeanUtils.copyProperties(model, semesterEntity);
        semesterService.save(semesterEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
    public ItooResult delete(String id){
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        semesterService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
    public ItooResult deleteByIds(List<String> ids){
        if (CollectionUtils.isEmpty(ids)) {
            return ItooResult.build(ItooResult.FAIL, "ids为空");
        }
        semesterService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model SemesterModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
    public ItooResult modify(SemesterModel model){
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        SemesterEntity semesterEntity = new SemesterEntity();
        BeanUtils.copyProperties(model, semesterEntity);
        semesterService.updateById(semesterEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Semester
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */

    @Override
    public ItooResult findById(String id){
        if (StringUtils.isEmpty(id)) {
            return ItooResult.build(ItooResult.FAIL, "id为空");
        }
        SemesterEntity semesterEntity = semesterService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterEntity);
    }

    /**
     * 分页查询所有Semester
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
    public ItooResult queryPageAll(Integer pageNo, Integer pageSize){
        PageInfo<SemesterEntity> semesters = semesterService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesters);
    }

    /**
     * 根据学年学期名称模糊查询semester
     *
     * @param name     学年学期名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的semester
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
   public ItooResult querySemesterByLikeName(String name, Integer pageNo, Integer pageSize){
        PageInfo<SemesterEntity> semesterList = semesterService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */




    /**
     * 无参查找Semester
     *
     * @return semesterEntityList
     * @author 杨江涛
     * @since 2.0.0 2018-11-17 11:17:35
     */
    @Override
    public ItooResult selSemesterId() {
        List<SemesterEntity> semesterEntityList = semesterService.selSemesterId();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", semesterEntityList);
    }
}
